const axios = require("axios");
const FormData = require("form-data");
const fetch = require("node-fetch");
const student_id = "2c92e4518c9fe9bd018cab84582d1ed7";
const JSESSIONID="CCDCDDDB9655D6314A016E837190360F";
const getStudentQuestionBank =
  "https://www.qdyp.com.cn/api/service/QuestionBank/getStudentQuestionBank.json?bust=1748591188309";
const getChapterEvaluateLis = `https://www.qdyp.com.cn/api/service/CourseEvaluate/getChapterEvaluateList.json?PageNo=1&PageSize=99&qry_student_id=${student_id}`;
const getExamQuestion =
  "https://www.qdyp.com.cn/api/edu/LearnVideo/getExamQuestion.json";
const andPaperEvaluate =
  "https://www.qdyp.com.cn/api/edu/TeacherEvaluate/andPaperEvaluate.json?bust=1717643602330";
  const Cookie = `Hm_lvt_938b95f13b49fe5f34a95053fd0fffd3=1748575324; HMACCOUNT=CDF9515EBBF2741A; Hm_lpvt_938b95f13b49fe5f34a95053fd0fffd3=1748591188;JSESSIONID=${JSESSIONID};`
const get = async (url) => {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        headers: {
          Cookie,
        },
      })
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
const post = async (url, data) => {
  return new Promise((resolve, reject) => {
    let ar = axios({
      method: "post",
      url,
      data,
      headers: {
        Cookie,
      },
    })
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
post(getStudentQuestionBank).then((res) => {
  res.Data.map((q) => {
    get(getChapterEvaluateLis + `&qry_course_id=${q.course_id}`).then(
      (getChapterEvaluateListRes) => {
        getChapterEvaluateListRes.Data.map((v) => {
          const evaluate_id = v.evaluate_id;
          const formData = new FormData();
          let obj = {
            evaluate_id: evaluate_id,
            course_id: q.course_id,
            evaluate_flag: "Homework",
            evaluate_object_id: "",
            semester_id: "2024-06-06 11:50:03",
          };
          let text =
            "evaluate_id=" +
            obj.evaluate_id +
            "&course_id=" +
            obj.course_id +
            "&evaluate_flag=" +
            obj.evaluate_flag +
            "&evaluate_object_id=" +
            obj.evaluate_object_id +
            "&semester_id=" +
            obj.semester_id;
          get(
            getExamQuestion +
              `?PageNo=1&PageSize=99&qry_evaluate_id=${evaluate_id}&qry_exclude_complete=`
          ).then((getExamQuestionRes) => {
            getExamQuestionRes.Data.map((v) => {
              const question_id = v.question_id;
              const right_key = v.right_key;
              const Options = v.Options.filter(
                (v) => v.right_answer === "true"
              );
              let evaluate_content = Options.map((v) => v.option_id).join(
                ","
              );
              if (!evaluate_content) {
                evaluate_content = right_key
              }
              text += "&question_id=" + question_id;
              text += "&evaluate_content=" + evaluate_content;
              text += "&image_server_id=";
            });
            fetch(
              "https://www.qdyp.com.cn/api/edu/TeacherEvaluate/andPaperEvaluate.json?bust=1717655417135",
              {
                headers: {
                  accept: "application/json, text/plain, */*",
                  "accept-language": "zh-CN,zh;q=0.9",
                  "content-type":
                    "application/x-www-form-urlencoded; charset=UTF-8",
                  "sec-ch-ua":
                    '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
                  "sec-ch-ua-mobile": "?0",
                  "sec-ch-ua-platform": '"Windows"',
                  "sec-fetch-dest": "empty",
                  "sec-fetch-mode": "cors",
                  "sec-fetch-site": "same-origin",
                  cookie:Cookie,
                  Referer:
                    "https://www.qdyp.com.cn/edu/service/financial/submitEvaluate.ftl?id=2c9201c58ec7e773018f145c49a6415a&cid=2c9201c58c244c0b018c334acc453ca8&sid=2c9201c58ec7e773018f145c499c4159&show=all",
                  "Referrer-Policy": "strict-origin-when-cross-origin",
                },
                body: text,
                method: "POST",
              }
            ).then(res=>{
              res.json().then(rs=>{
                console.log(q.course_name,rs)
              })
            });
          });
        });
      }
    );
  });
});
